//
//  IntolawyerViewController.m
//  e律师
//
//  Created by nykj-mac-03 on 2017/6/7.
//  Copyright © 2017年 test. All rights reserved.
//

#import "IntolawyerViewController.h"

@interface IntolawyerViewController ()<JHPickerDelegate>



@property (nonatomic, strong) NSMutableArray *lawyerArray;

@property (nonatomic, strong) NSArray *dataSource1;
@property (nonatomic, strong) NSArray *dataSource2;
@property (nonatomic, strong) UIView *caseView;
@property (nonatomic, strong) UIImageView *titleImageView;
@property (nonatomic, strong) UILabel *caseTitleLable;
@property (nonatomic, strong) UIView *lawyerView;
@property (nonatomic, strong) UIImageView *lawyerImageView;
@property (nonatomic, strong) UILabel *lawyerTitleLable;
@property (nonatomic, strong) UILabel *lawyerLable;
@property (nonatomic, strong) UIButton *chooseLawyer;
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) UILabel *lawyer;
@property (nonatomic, strong) UIButton *submitButton;

@property (nonatomic, strong) UILabel *caseNumber;
@property (nonatomic, strong) UILabel *saDate;
@property (nonatomic, strong) UILabel *sffs;
@property (nonatomic, strong) UILabel *dlf;

@property (nonatomic, assign) NSInteger seletedIndex;
@end

@implementation IntolawyerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.barTitleLable.text = @"变更律师";
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource1 = [NSArray array];
    self.dataSource2 = [NSArray array];
    self.lawyerArray = [NSMutableArray array];
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self getUI];
    [self getData];
}
- (void)getData{
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
        //请求1
        NSLog(@"Request_1");
        
        [NetworkingManager getUrl:[NSString stringWithFormat:@"%@id=%@",INTOCHANGELWYER_URL,self.intoId] dic:nil Succeed:^(id responseData) {
            NSLog(@"%@",responseData);
            self.dataSource1 = responseData[@"result"];
            self.caseTitleLable.text = self.dataSource1[0][@"ay"];//后期传值
            
            [UIView animateWithDuration:2 animations:^{
                // 控件 alpha 设成0
                self.caseNumber.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.0f];
                self.saDate.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.0f];
                self.sffs.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.0f];
                self.dlf.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.0f];
                
            } completion:^(BOOL finished) {
                // 对控件赋值
                self.caseNumber.text = self.dataSource1[0][@"ah_number"];
                self.saDate.text = self.dataSource1[0][@"sarq"];
                self.sffs.text = self.dataSource1[0][@"sffs"];
                self.dlf.text = self.dataSource1[0][@"dlf"];
            }];
            

            
        } failure:^(NSError *error) {
            
        }];
    });
    dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
        //请求2
        NSLog(@"Request_2");
        
        [NetworkingManager getUrl:[NSString stringWithFormat:@"%@company_id=%@",CHOOSELAWYER_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"cid"]] dic:nil Succeed:^(id responseData) {
            NSLog(@"%@",responseData);
            self.dataSource2 = responseData[@"result"];
            for (NSDictionary *dic  in self.dataSource2) {
                [self.lawyerArray addObject:dic[@"name"]];
                [self.chooseLawyer addTarget:self action:@selector(action_ChooseLawyer:) forControlEvents:UIControlEventTouchUpInside];
                NSLog(@"lawyerNameArr = %@",self.lawyerArray);
            }

        } failure:^(NSError *error) {
            
        }];
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        //界面刷新
        NSLog(@"任务均完成，刷新界面");
        
    });
}
- (void)getUI{
    [self.view addSubview:self.lawyerView];
    [self.view addSubview:self.caseView];
    [self.view addSubview:self.lawyerLable];
    [self.view addSubview:self.chooseLawyer];
    [self.chooseLawyer addSubview:self.image];
    [self.chooseLawyer addSubview:self.lawyer];
    
    [self.caseView addSubview:self.caseTitleLable];
    [self.caseView addSubview:self.titleImageView];
    [self.lawyerView addSubview:self.lawyerImageView];
    [self.lawyerView addSubview:self.lawyerTitleLable];
    
    for (int i = 0 ; i <= 3; i ++) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 120 + i * 30, 70, 20)];
        lable.tag = 100 + i;
        lable.font = [UIFont systemFontOfSize:12];
        if (i == 0) {
            lable.text = [NSString stringWithFormat:@"案       号： "];
        }
        else if (i == 1) {
            lable.text = [NSString stringWithFormat:@"收案日期： "];
        }
        else if (i == 2) {
            lable.text = [NSString stringWithFormat:@"收费方式： "];
        }
        else if (i == 3) {
            lable.text = [NSString stringWithFormat:@"代  理  费： "];
        }
        [self.view addSubview:lable];
    }
    
    [self.view addSubview:self.caseNumber];
    [self.view addSubview:self.saDate];
    [self.view addSubview:self.sffs];
    [self.view addSubview:self.dlf];
    [self.view addSubview:self.submitButton];
}
#pragma mark - UITextFieldDelegate

-(void)PickerSelectorIndixString:(NSString *)str
{
    if ([self.lawyerArray containsObject:str]) {
        self.seletedIndex = [self.lawyerArray indexOfObject:str];
        NSLog(@"index = %ld",self.seletedIndex);
    }
    UILabel *lawyer = [self.view viewWithTag:1000];
    lawyer.text = str;
}
#pragma mark - ButtonTarget
- (void)action_Submit:(UIButton *)sender{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkingManager postUrl:ALTERLAWYER_URL dic:@{@"create_id":self.dataSource2[self.seletedIndex][@"id"],@"id":self.intoId,@"update_date":@"2017-08-02"} Succeed:^(id responseData) {
        NSLog(@"%@",responseData);
        self.dataSource2 = responseData[@"result"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
- (void)action_ChooseLawyer:(UIButton *)sender{
    JHPickView *picker = [[JHPickView alloc] initWithFrame:self.view.bounds];
    picker.delegate = self ;
    picker.selectLb.text = @"请选择";
    picker.customArr = self.lawyerArray;
    [self.view addSubview:picker];
}
#pragma mark - Getter

- (UIView *)caseView{
    if (!_caseView) {
        _caseView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 50)];
        _caseView.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
    }
    return _caseView;
}
- (UIImageView *)titleImageView{
    if (!_titleImageView) {
        _titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 15, 20, 20)];
        _titleImageView.image = [UIImage imageNamed:@"my.png"];
        //    titleImageView.backgroundColor = [UIColor redColor];

    }
    return _titleImageView;
}
- (UILabel *)caseTitleLable{
    if (!_caseTitleLable) {
        _caseTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(55, 10, 240, 40)];
        _caseTitleLable.text = @"ay";//后期传值
        _caseTitleLable.textColor = [UIColor blackColor];
        _caseTitleLable.font = [UIFont systemFontOfSize:15];
    }
    return _caseTitleLable;
}
- (UIView *)lawyerView{
    if (!_lawyerView) {
        _lawyerView = [[UIView alloc] initWithFrame:CGRectMake(0, 240, self.view.bounds.size.width , 50)];
        _lawyerView.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        //    lawyerView.layer.cornerRadius = 5;
        //    lawyerView.layer.masksToBounds = YES;

    }
    return _lawyerView;
}
- (UIImageView *)lawyerImageView{
    if (!_lawyerImageView) {
        _lawyerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 15, 20, 20)];
        _lawyerImageView.image = [UIImage imageNamed:@"my.png"];
        //    lawyerImageView.backgroundColor = [UIColor redColor];
    }
    return _lawyerImageView;
}
- (UILabel *)lawyerTitleLable{
    if (!_lawyerTitleLable) {
        _lawyerTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(55, 5, 240, 40)];
        
        _lawyerTitleLable.text = @"办案律师 ";//后期传值
        _lawyerTitleLable.textColor = [UIColor blackColor];
        _lawyerTitleLable.font = [UIFont systemFontOfSize:15];
    }
    return _lawyerTitleLable;
}
- (UILabel *)lawyerLable{
    if (!_lawyerLable) {
        _lawyerLable = [[UILabel alloc ]initWithFrame:CGRectMake(5, 307, 80, 20)];
        _lawyerLable.text = @"律       师： ";
        _lawyerLable.font = [UIFont systemFontOfSize:13];
    }
    return _lawyerLable;
}
- (UIButton *)chooseLawyer{
    if (!_chooseLawyer) {
        _chooseLawyer = [[UIButton alloc] initWithFrame:CGRectMake(100, 305, 180, 25)];
        _chooseLawyer.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
        _chooseLawyer.layer.borderColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f].CGColor;
        _chooseLawyer.layer.borderWidth = 1.0f;
        _chooseLawyer.layer.cornerRadius = 5;
        _chooseLawyer.layer.masksToBounds = YES;
        
    }
    return _chooseLawyer;
}
- (UIImageView *)imaage{
    if (!_image) {
        _image = [[UIImageView alloc] initWithFrame:CGRectMake(160, 0, 20, 25)];
        _image.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
        _image.image = [UIImage imageNamed:@"三角"];
    }
    return _image;
}
- (UILabel *)lawyer{
    if (!_lawyer) {
        _lawyer = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 140, 25)];
        _lawyer.text = self.lawyerName;
        _lawyer.tag = 1000;
        _lawyer.font = [UIFont systemFontOfSize:13];
    }
    return _lawyer;
}
- (UILabel *)caseNumber{
    if (!_caseNumber) {
        _caseNumber = [[UILabel alloc] initWithFrame:CGRectMake(90, 120, 200, 20)];
        _caseNumber.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
        _caseNumber.tag = 200;
        _caseNumber.font = [UIFont systemFontOfSize:12];
    }
    return _caseNumber;
}
- (UILabel *)saDate{
    if (!_saDate) {
        _saDate = [[UILabel alloc] initWithFrame:CGRectMake(90, 150, 200, 20)];
        _saDate.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
        _saDate.tag = 201;
        _saDate.font = [UIFont systemFontOfSize:12];
    }
    return _saDate;
}
- (UILabel *)sffs{
    if (!_sffs) {
        _sffs = [[UILabel alloc] initWithFrame:CGRectMake(90, 180, 200, 20)];
        _sffs.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
        _sffs.tag = 202;
        _sffs.font = [UIFont systemFontOfSize:12];
    }
    return _sffs;
}
- (UILabel *)dlf{
    if (!_dlf) {
        _dlf = [[UILabel alloc] initWithFrame:CGRectMake(90, 210, 200, 20)];
        _dlf.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
        _dlf.tag = 203;
        _dlf.font = [UIFont systemFontOfSize:12];
    }
    return _dlf;
}

- (UIButton *)submitButton{
    if (!_submitButton) {
        _submitButton = [[UIButton alloc] initWithFrame:CGRectMake(50,350, SCREEN_WIDTH - 100, 50)];
        _submitButton.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
        [_submitButton setTitle:@"确认提交" forState:0];
        
        _submitButton.titleLabel.textColor = [UIColor whiteColor];
        
        [_submitButton addTarget:self action:@selector(action_Submit:) forControlEvents:UIControlEventTouchUpInside];
        _submitButton.layer.cornerRadius = 5;
        _submitButton.layer.masksToBounds = YES;
    }
    return _submitButton;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
